package cn.superchart.web.core.handler;

import cn.hutool.core.date.DateUtil;
import cn.superchart.common.base.BaseUserDetails;
import cn.superchart.common.utils.CacheContext;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.ApplicationListener;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.authentication.event.AuthenticationSuccessEvent;
import org.springframework.stereotype.Component;
@Slf4j
@Component
public class ScAuthenticationSuccessHandler implements ApplicationListener<AuthenticationSuccessEvent> {

    @Override
    public void onApplicationEvent(AuthenticationSuccessEvent event) {
        UsernamePasswordAuthenticationToken authenticationToken = (UsernamePasswordAuthenticationToken) event.getSource();
        BaseUserDetails userDetails = (BaseUserDetails)authenticationToken.getPrincipal();
        CacheContext.putUserCache(userDetails.getUserId(),userDetails);
        log.info("User {} logged at {}  successfully.",userDetails.getUsername(), DateUtil.now());
    }
}
